import { createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw } from 'vue-router'

const PrivateLayout = () =>
  import(/* webpackChunkName: "PrivateLayout" */ '../views/PrivateLayout.vue')

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'index',
    redirect: 'login'
  },
  {
    path: '/login',
    name: 'login',
    component: () =>
      import(/* webpackChunkName: "login" */ '../views/LoginView.vue')
  },
  {
    path: '/systemmanage',
    name: 'systemmanage',
    component: PrivateLayout,
    children: [
      {
        path: 'userlist',
        name: 'userList',
        component: () => import('../views/systemManage/UserList.vue')
      },
      {
        path: 'rolelist',
        name: 'roleList',
        component: () =>
          import(
            /* webpackChunkName: "roleList" */ '@/views/systemManage/RoleList.vue'
            )
      }
    ]
  },
  {
    path: '/repayschememanage',
    name: 'repaySchemeManage',
    component: PrivateLayout,
    children: [
      {
        path: 'schemelist',
        name: 'schemeList',
        component: () =>
          import(
            /* webpackChunkName: "schemeList" */ '@/views/repayScheme/RepaySchemeList.vue'
            )
      },
      {
        path: 'newscheme',
        name: 'newScheme',
        component: () =>
          import(
            /* webpackChunkName: "newScheme" */ '@/views/repayScheme/NewRepayScheme.vue'
            )
      }
    ]
  },
  {
    path: '/productmanage',
    name: 'productManage',
    component: PrivateLayout,
    children: [
      {
        path: 'configlist',
        name: 'productConfigList',
        component: () =>
          import(
            /* webpackChunkName: "productConfigList" */ '@/views/productManage/ProductConfigList.vue'
            )
      }
    ]
  },
  {
    path: '/ordermanage',
    name: 'orderManage',
    component: PrivateLayout,
    children: [
      {
        path: 'orderlist',
        name: 'orderList',
        component: () =>
          import(
            /* webpackChunkName: "orderList" */ '@/views/orderManage/OrderList.vue'
            )
      }
    ]
  },
  {
    path: '/collectmanage',
    name: 'collectManage',
    component: PrivateLayout,
    children: [
      {
        path: 'collectlist',
        name: 'collectList',
        component: () =>
          import(
            /* webpackChunkName: "collectList" */ '@/views/collectManage/CollectionList.vue'
            )
      },
      {
        path: 'collectdetail',
        name: 'collectDetail',
        component: () =>
          import(
            /* webpackChunkName: "collectDetail" */ '@/views/collectManage/CollectionOrderDetail.vue'
            )
      }
    ]
  },
  {
    path: '/welcome',
    name: 'welcome',
    component: PrivateLayout,
    children: [
      {
        path: '/welcome',
        name: 'welcome',
        component: () =>
          import(/* webpackChunkName: "welcome" */ '../views/WelcomeView.vue')
      }
    ]
  },
  {
    path: '/nopermission',
    name: 'noPermission',
    component: PrivateLayout,
    children: [
      {
        path: '/nopermission',
        name: 'noPermission',
        component: () =>
          import(/* webpackChunkName: "noPermission" */ '../views/NoPermissionView.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = !!localStorage.getItem('userName')
  if (to.name !== 'login' && !isLoggedIn) {
    next({ name: 'login' })
  } else {
    next()
  }
})

export default router
